package cn.tude.boot22.controller;

import cn.tude.boot22.entity.Hero;
import cn.tude.boot22.mapper.HeroMapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.ResultSet;
import java.util.List;

/**
 * @Classname: HeroController
 * @Author: bromide
 * @CreateTime: 2022/5/11--15:32
 * @Version: V1.0
 * @Description:
 */
@Controller
public class HeroController {
    //此注解的作用:Spring框架结合Mybatis狂降会自动将HeroMapper生成一个实现类和实现里面的方法
    //而且会自动实例化该对象  required = false  告诉idea编译器此对象是非必须的.
    @Autowired(required = false)
    HeroMapper mapper;

    @RequestMapping("/add")
    @ResponseBody
    public String add(Hero hero){
        System.out.println("hero = "+hero);
        mapper.insert(hero);
        return "添加完成";
    }
    @RequestMapping("/delete")
    @ResponseBody
    public String delete(String name){
        mapper.deleteByName(name);
        return "删除完成!";
    }
    @RequestMapping("/update")
    @ResponseBody
    public String update(Hero hero){
        mapper.updateById(hero);
        return "修改完成!";
    }
    @RequestMapping("/select")
    @ResponseBody
    public String select(){
        List<Hero> list = mapper.select();

        return list.toString()+"";
    }
}
